package pl.edu.agh.io.gpstracker.client.window;

import pl.edu.agh.io.gpstracker.client.action.LoginAction;
import pl.edu.agh.io.gpstracker.client.dto.UserDto;
import pl.edu.agh.io.gpstracker.client.result.ObjectResult;
import pl.edu.agh.io.gpstracker.client.widget.MainScreenWidget;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class LoginWindow extends GenericWindow {

	private static final String PASSWORD = "PASSWORD";
	private static final String LOGIN = "LOGIN";

	//fakowa sesja :)
	public static UserDto LOGED_USER;
	
	private DynamicForm form;
	private IButton logInButton;
	private boolean isLoged = false;

	// bleeee
	private MainScreenWidget root;

	public LoginWindow(MainScreenWidget root) {
		super(250, 125);
		super.setTitle("GPS Tracker Logowanie");
		super.setMargin(10);
		this.root = root;
		init();
	}

	/**
	 * 
	 */
	private void init() {
		VLayout layout = new VLayout();
		form = new DynamicForm();
		// form.setNumCols(1);
		// moze fabryka ?
		TextItem login = new TextItem(LOGIN, "Login");
		login.setRequired(true);

		PasswordItem password = new PasswordItem(PASSWORD, "Haslo");
		password.setRequired(true);
		form.setFields(login, password);

		HLayout bu = new HLayout();
		logInButton = new IButton("Zaloguj");
		logInButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (isLoged) {
					isLoged = false;
					form.setVisible(!isLoged);
					logInButton.setTitle("Zaloguj");
					form.clearValues();
					logInButton.setWidth(100);
					isLoged = false;
					root.lockTabs(!isLoged);
					SC.say("Zostales poprawnie wylogowany!");
				} else if (form.validate()) {
					login();
				}
			}
		});
		bu.addMember(logInButton);
		bu.setAlign(Alignment.RIGHT);
		form.setAlign(Alignment.CENTER);
		layout.setAlign(Alignment.CENTER);
		layout.setMembers(form, bu);
		addItem(layout);
	}

	private void login() {
		dispatcher.execute(new LoginAction(form.getValueAsString(LOGIN), form
				.getValueAsString(PASSWORD)),
				new AsyncCallback<ObjectResult<UserDto>>() {

					@Override
					public void onFailure(Throwable caught) {
						SC.say(caught.toString());
					}

					// FAKE!!
					@Override
					public void onSuccess(ObjectResult<UserDto> result) {
						if (result.getResult() == null) {
							SC
									.warn("Niepoprawna nazwa uzytkownika lub haslo. ");
						} else {
							isLoged = true;
							root.lockTabs(!isLoged);
							LoginWindow.this.hide();
							LOGED_USER = result.getResult();
						}
					}
				});
	}

}
